import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Start {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) throws AWTException, InterruptedException {
        int w = 1700;
        int h = 1050;
        System.out.println("开始录屏配置");
        System.out.println("输入屏幕大小，不得为单数");
        System.out.println("宽：（默认1700）");
        String s1 = sc.nextLine();
        if (s1 != null && s1.trim().length() > 0) {
            w = Integer.parseInt(s1.trim());
        }
        System.out.println("高：（默认1050）");
        String s2 = sc.nextLine();
        if (s2 != null && s2.trim().length() > 0) {
            h = Integer.parseInt(s2.trim());
        }
        System.out.println("开始录屏");

        TH0 th0 = new TH0(w, h);
        th0.start();
        while (THI.b) {
            System.out.println("输入y退出：");
            String line = sc.nextLine();
            boolean equals = !"y".equals(line);
            THI.b = equals;
        }

        while (!TH0.service.isTerminated()) {
            Thread.sleep(1000);
        }
        System.out.println("===================");
        System.out.println("录屏结束，开始生成");
        ExecutorService service = Executors.newFixedThreadPool(4);
        service.execute(new THff());
        Thread.sleep(filePaths("./ev/").size() * 10L);
        System.out.println("===================");
        System.out.println("生成结束，开始清理");
        List<String> list = filePaths("./ev/", ".png");
        for (String s : list) {
            new File(s).delete();
        }
        System.out.println("==========完成=======");
    }

    public static java.util.List<String> filePaths(String files) {
        ArrayList<String> list = new ArrayList<>();
        filePath(list, new File(files));
        return list;
    }

    //获取某目录文件路径集合,匹配相关文件： files为入参目录路径，name为只要文件路径中包含name的文件
    public static java.util.List<String> filePaths(String files, String name) {
        ArrayList<String> list = new ArrayList<>();
        filePath(list, new File(files), name);
        return list;
    }


    private static void filePath(java.util.List<String> list, File files) {
        for (File file : files.listFiles()) {
            if (file.isFile()) {
                list.add(file.getPath());
            } else {
                filePath(list, file);
            }
        }
    }

    private static void filePath(List<String> list, File files, String name) {
        for (File file : files.listFiles()) {
            if (file.isFile()) {
                if (file.getPath().contains(name)) {
                    list.add(file.getPath());
                }
            } else {
                filePath(list, file, name);
            }
        }
    }

}
